<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '280px'
    },
    dataList:Object
  },
  data() {
    return {
      chart: null,
      xAxisData: [],
      data1: [],
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  watch: {
    dataList: {
      immediate: true,
      deep: true,
      handler(data) {
        console.log(data.xAxisData,"YYYY")
        this.xAxisData = data.xAxisData;
        this.data1 = data.seriesData;
        this.$nextTick(() => {
          if (this.chart) {
            this.chart.dispose()
            this.chart = null
          }
          this.initChart()
        })
      }
    }
  },
  methods: {
    initChart() {

      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption(
        {
          tooltip: {
            trigger: 'item'
          },
          grid: {
            top: '1%',
            bottom: '20%',
            right: '2%',
            left: '2%'
          },
          xAxis: {
            data: this.xAxisData,
            axisTick: {
              show: false
            },
            axisLine: {
              show: false
            },
            axisLabel: {
              interval: 0,
              textStyle: {
                color: '#fff',
                fontSize: 20
              },
              margin: 40
            }
          },
          yAxis: {
            splitLine: {
              show: false
            },
            axisTick: {
              show: false,
              alignWithLabel: true,
              inside: true
            },
            axisLine: {
              show: false
            },
            axisLabel: {
              show:false,
              textStyle: {
                color: '#fff',
                fontSize: 20
              }
            }
          },
          series: [
            // 底部的光晕
            {
              name: '',
              type: 'pictorialBar',
              tooltip: {
                show: false
              },
              symbolSize: [90, 40],
              symbolOffset: [0, 20],
              z: 1,
              itemStyle: {
                normal: {
                  color: 'transparent',
                  borderColor: '#26B2E8',
                  borderType: 'solid',
                  borderWidth: 10
                }
              },
              data: this.data1
            },
            {
              name: '',
              type: 'pictorialBar',
              tooltip: {
                show: false
              },
              symbolSize: [70, 20],
              symbolOffset: [0, 10],
              z: 1,
              itemStyle: {
                normal: {
                  color: 'transparent',
                  borderColor: '#26B2E8',
                  borderType: 'solid',
                  borderWidth: 10
                }
              },
              data: this.data1
            },
            {
              name: '品种',
              type: 'bar',
              barWidth: 45,
              barGap: '-100%',
              itemStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: 'rgba(176, 255, 234, 0)'
                  },
                  {
                    offset: 1,
                    color: 'rgba(176, 255, 234, 1)'
                  }
                ])
              },
              data: this.data1
            },
            //头部、中部、尾部圆片
            {
              name: '品种',//中部
              type: 'pictorialBar',
              symbolSize: [45, 25],
              symbolOffset: [0, -10],
              z: 12,
              symbolPosition: 'end',
              itemStyle: {
                opacity: 1,
                color: '#58F1B6',
              },
              data: this.data1,
              label: {
                show: true,
                position: 'top', // 顶部显示
                formatter: '{c}', // 显示数据值
                fontSize: 12,
                color: 'rgba(140, 217, 197, 1)'
              },
            },
            {
              //三个最低下的圆片
              name: '',
              type: 'pictorialBar',
              symbolSize: [45, 25],
              symbolOffset: [0, 10],
              z: 12,
              itemStyle: {
                opacity: 1,
                color: '#58F1B6'
              },
              data: this.data1
            }
          ],
          backgroundColor: 'rgba(0,0,0,0)'
        }
      )
    }
  }
}
</script>
